package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.controller.vo.EarlyWaringVO;
import com.k2data.k2app.domain.po.EarlyWaringDO;
import com.k2data.k2app.domain.po.EarlyWaringDTO;
import com.k2data.k2app.mapper.EarlyWaringMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.EarlyWarningService;
import com.k2data.k2app.service.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 预警消息
 * Created by clb on 17-3-17.
 * </pre>
 */
@Api(description = "预警处理")
@RestController
@RequestMapping("/warings")
public class EarlyWarningController {

    @Autowired
    private EarlyWarningService earlyWarningService;
    @Autowired
    private EarlyWaringMapper earlyWaringMapper;
    @Autowired
    private SystemService systemService;

    /**
     * 查询预警信息
     * <pre>
     * dealType=20:查询除去"忽略"的其他预警
     * 0:站内未处理
     * 1:忽略
     * 2:生成工单
     * 3:不显示
     * 4:发送短信
     * 5:发送邮件
     * 6:不处理的
     * </pre>
     * @param pageNum
     * @param pageSize
     * @param deviceNo
     * @param taskType
     * @param message
     * @param status
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "预警查询(带分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "预警类型,实时预警:real 故障预警:fault", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "owner", value = "用户id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "处理方式", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "message", value = "故障描述", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deviceNo", value = "设备编号", dataType = "String", paramType = "query")
    })
    @GetMapping(value = "")
    public ResponseEntity<CommonResultResponse> page(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                     @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                     @RequestParam(value = "deviceNo", required = false) String deviceNo,
                                                     @RequestParam(value = "owner", required = false) Long owner,
                                                     @RequestParam(value = "type", required = false) String taskType,
                                                     @RequestParam(value = "message", required = false) String message,
                                                     @RequestParam(value = "status", required = false) String status) throws Exception {
        EarlyWaringDO earlyWaringDO = new EarlyWaringDO();
        earlyWaringDO.setType(taskType);
        earlyWaringDO.setDeviceNo(deviceNo);
        if(owner!=null && !"".equals(owner)){
            // 权限过滤，管理员不加权限过滤功能，默认所有权限
            Map role = systemService.getRolesByUser(owner);
            if(role.get("enname")!=null && !role.get("enname").toString().equals("Administrator")){
                 earlyWaringDO.setOwner(owner);
            }
        }
        EarlyWaringDTO.setWarningType(earlyWaringDO,status);
        earlyWaringDO.setMessage(message);


        int pageNumReal = pageNum == null ? 1 : pageNum;
        int pageSizeReal = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;

        int i = earlyWaringMapper.selectCount(earlyWaringDO);
        PageHelper.startPage(pageNumReal, pageSizeReal);
        PageInfo pageInfo = new PageInfo(pageNumReal, pageSizeReal, i);

        earlyWaringDO.setPageNum(pageNumReal);
        earlyWaringDO.setPageSize(pageSizeReal);
        List<EarlyWaringDO> warings = earlyWarningService.list(earlyWaringDO);
        return ResponseHelper.success(warings,pageInfo);
    }

    @ApiOperation(value = "预警处理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "操作类型:ignore(忽略) order(生成工单)", dataType = "String", paramType = "query")
    })
    @PutMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> deleted(@PathVariable("id") Long id,
                                                        @RequestParam(value = "type", required = false) String type) throws Exception {
        EarlyWaringDO earlyWaringDO = new EarlyWaringDO();
        earlyWaringDO.setId(id);
        earlyWaringDO.setDealTypeName(type);
        return ResponseHelper.success(earlyWarningService.dealStatusV1(earlyWaringDO));
    }

    @ApiOperation(value = "预警聚合")
    @GetMapping(value = "/count")
    public ResponseEntity<CommonResultResponse> countGroupLevel() throws Exception {
        List<EarlyWaringDO> count = earlyWarningService.count();
        List<EarlyWaringVO> list = Lists.newArrayList();
        count.forEach(s->{
            list.add(new EarlyWaringVO(s.getLevel(),s.getCount(),s.getExt()));

        });

        return ResponseHelper.success(list);
    }

}
